<template>
    <view class="commonList marb-20" :class="me.loginUserType==$enums.RefreshToken_LoginType.集团 ? 'showTabar':'hideTabar'">
        <u-sticky>
            <view class="setAllRead">
                <u-button type="primary" class="fr marr-30" size="small" @click="setAllIsRead">一键标注已读</u-button>
            </view>
        </u-sticky>
        <view class="cons-ul">
            <view class="cons-li" v-for="(item,index) in list" :key="index" @click="lookNotice(item)">
                <u-row class="rowBox title marb-10" >
                    <u-col span="1">
						<u-icon name="volume-fill" size="20" class="notice" :color="item.status==200?'#CDCDCD':'#1296DB'"></u-icon>
                    </u-col>
                    <u-col span="8">
                        <text>{{item.title}}</text>
                    </u-col>
					<u-col span="3">
						<u-tag :text="getText(item)" type="warning" plain  size="mini"/>
					</u-col>
                </u-row>
            </view>
            <u-empty mode="message" v-if="!list || !list.length"></u-empty>
        </view>
		<u-toast ref="uToast"></u-toast>
    </view>
</template>

<script>
    export default {
        data() {
            return {
				me:getApp().globalData,
                list: [],
                //查询分页所需要的数据
                pages: {
                    pageIndex: 1,
                    pageSize: 20,
                    orderBy: "MessageID DESC",
                },
                filterData: {
                    _UserID: getApp().globalData.userID,
                    _IsDelete: false
                }
            }
        },
        //触底
        onReachBottom() {
            if (this.pages.pageIndex) {
                this.GetPage()
            } else {
                this.$u.toast('没有更多了')
            }
        },
        onShow() {
            this.RefreshData()
        },
        onPullDownRefresh() { // 下拉刷新
            this.RefreshData()
            uni.stopPullDownRefresh();
        },
        methods: {
			getText(item){
				let {type}={...item};
				if(type==1){return '备绳维保'}
				if(type==2){return '在用绳检测'}
			},
            RefreshData(flag) {
                this.pages.pageIndex = 1;
                this.GetPage(flag);
            },
            //分页
            GetPage(flag) {
                uni.showLoading();
                this.$app.CallApi(this.$app.api.Message.GetPage(this.pages.pageIndex, this.pages.pageSize, this.pages.orderBy), {
                    data: this.filterData,
                    async: false,
                    success: (res) => {
                        this.pages.pageIndex == 1 ? this.list = res.items : this.list = [...this.list, ...res.items];
						console.log(this.list);
						const that=this;
                        res.items.length < this.pages.pageSize ? this.pages.pageIndex = 0 : this.pages.pageIndex++
                        uni.hideLoading();
                        if (flag) {
                            this.$u.toast('操作完成')
                        }
                    }
                });
            },
            //点击消息
            lookNotice(row) {
                //判断是否未读，调用已读接口
				let data=JSON.parse(row.data)||{};
                if (!row.isRead) {
                    this.$app.CallApi(this.$app.api.Message.CanJump(row.messageID), {
                        success: (res) => {
							if(res){
								let url='/pages/spareRopeMaintenance/index';
								if(row.type==2){
									url='/pages/ropeDetection/index';
								}
								url+=`?code=${data.code}`;
								uni.navigateTo({
								    url: url
								})
								return;
							}
							this.$refs.uToast.show({
								type: 'error',
								title: '',
								message: "暂无模块权限，请联系集团管理员！"
							})
							return;
                        }
                    });
                }
				return;
                //身份权限校验
                let authorityList = uni.getStorageSync(this.$config.permissionKey);
                let perID = null
                let jsonData = row.dataJsonObj
                let params = '?id=' + jsonData.id + '&code='
                let url = '/pages/workbench/'
                if (row.operTypeID == this.$enums.Message_Type.派工) {
                    url += 'reportAssign/reportAssign';
                    params += jsonData.assignCode
                    perID = 40400;
                } else if (row.operTypeID == this.$enums.Message_Type.质检确认) {
                    url += 'check/check';
                    params += jsonData.batchCode
                    perID = 40600;
                } else if (row.operTypeID == this.$enums.Message_Type.打回) {
                    if (jsonData.isAssign) {
                        url += 'reportAssign/reportAssign';
                        params += jsonData.assignCode
                        perID = 40400;
                    } else {
                        url += 'reportProcess/reportProcess';
                        params += jsonData.batchCode
                        perID = 40500;
                    }
                }
                let idx = authorityList.findIndex(x => {
                    return x == perID;
                });
                if (idx == -1) {
                    this.$app.Tips({
                        title: "暂无权限！"
                    })
                    return
                }
                uni.navigateTo({
                    url: url + params
                })
            },
            //一键已读
            setAllIsRead() {
                let that = this
                this.$app.CallApi(this.$app.api.Message.SerAllRead, {
                    data: {
                        _IsDelete: false,
                        _UserID: this.filterData._UserID
                    },
                    success: (res) => {
                        that.RefreshData(true);
                    }
                });
            }
        }
    }
</script>

<style scoped lang="scss">
    .setAllRead {
        background-color: white;
        height: 70rpx;

        .u-button {
            width: 180rpx;
            font-size: 24rpx;
            white-space: nowrap;
        }
    }

    text {
        word-break: break-all;
    }
	.rowBox ::v-deep .u-tag{
		justify-content: center;
	}
</style>